# -*- coding: utf-8 -*-
# @Time    : 2024/8/28 19:11
# @Author  : Alvin
# @File    : alvin.py

from app import create_app
from app.libs.error import APIException
from werkzeug.exceptions import HTTPException

from app.libs.error_code import ServerError

app = create_app()

# 全局异常处理
@app.errorhandler(Exception)
def framework_error(e):
    if isinstance(e, APIException):
        return e
    if isinstance(e, HTTPException):
        code = e.code
        msg = e.description
        error_code = 1007
        return APIException(msg, code, error_code)
    else:
        # 调试模式
        # log
        if not app.config['DEBUG']:
            return ServerError()
        else:
            raise e


if __name__ == '__main__':
    app.run(debug=app.config.get('DEBUG', True))